﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.FreeType;

namespace AvantGameMachine.Graphics
{
    public class FontChar
    {
        private uint charCode = 0;
        private Font parentFont = null;
        private Texture texture = null;
        private float xProp = 1.0f;
        private float yProp = 1.0f;
        private Material tMap;
        private int BRows;
        private int BWidth;
        private int BLeft;
        private int BTop;
        private Size2D advance;

        internal FontChar(uint _charCode, Font _font,Texture _t,ref FT_Bitmap _bitmap,FT_GlyphSlotRec _glyph)
        {
            parentFont = _font;
            texture = _t;
            charCode = _charCode;
            CreateCharObject(_bitmap,_glyph);
        }

        private void CreateCharObject(FT_Bitmap _bitmap,FT_GlyphSlotRec _glyph)
        {
            xProp = (float)_bitmap.width / (float)texture.Size.Width;
            yProp = (float)_bitmap.rows / (float)texture.Size.Height;
           
            tMap = new Material(texture);
            tMap.SetVertex(0,new PosVector(xProp,0,0));
            tMap.SetVertex(1, new PosVector(xProp, yProp, 0));
            tMap.SetVertex(2,new PosVector(0,yProp,0));
            tMap.SetVertex(3,new PosVector(0,0,0));

            BRows = _bitmap.rows;
            BWidth = _bitmap.width;
            BTop = _glyph.bitmap_top - _bitmap.rows;
            BLeft = _glyph.bitmap_left;
            advance = new Size2D(_glyph.advance.x >> 6, _glyph.advance.y >> 6);
        }

        internal Size2D LenProp
        {
            get { return new Size2D(xProp, yProp); }
        }

        internal Size2D BLen
        {
            get { return new Size2D(BWidth, BRows); }
        }

        internal Size2D BTopLeft
        {
            get { return new Size2D(BLeft, BTop); }
        }

        internal Texture CharTexture
        {
            get { return tMap.MapTexture; }
        }

        internal Material CharMapping
        {
            get { return tMap; }
        }

        internal Size2D Advance
        {
            get { return advance; }
        }
    }
}
